/**
 ******************************************************************************
 * File: json_crud_array.php
 * Create Date: November 18, 2011
 * @description: This json_crud_array is used for handling Create, Read, Update, Remove and Search operation of of special type of json array * 
 * @author Md. Ruzdi Islam   
 * ***************************************************************************
*/



/**
 ******************************************************************************
 * Create Date: November 18, 2011
 * @description: This is a special Type of json array on which we are goiing to  handling Create, Read, Update, Remove and Search operation
 * @example: example of this array is as following. (We are going to handling only "myjson_array.array" ) .
 *      var myjson_array = {    
 *           name : "name",    
 *           value: "value",  
 *           key  : "key",
 *           array: [
 *              { key:1 , .... },
 *              { key:2 , .... },
 *              { key:3 , .... }
 *           ],  
 *           matches: []
 *       };
 * **remember hre  on "myjson_array.array" array there must be a index named as "key" by which we going to make a search operation because we aregoing to add search operation by key
 * 
 * ***** also check json_crud_array.html for testing its demo use
 * 
 * @author Md. Ruzdi Islam  
 * ***************************************************************************
*/
var myjson_array = {    
    name : "name",    
    value: "value",  
    key  : "key",
    array: [],   // this array (myjson_array.array) format should be like - [ {key:1, .... }] ;
    matches: []
};

/**
 ******************************************************************************
 * Create Date: November 18, 2011
 * @description: This is a json_crud_array object where all functions are decleared
 * @example: example of this array is as following. 
 *      #prepare a node to add in the array remember "key" is mandatory index
 *            newnode = {
 *               key: 'ruzdi',
 *               title: "Test title 1"
 *           };
 *           
 *      #getting the new node index
 *          var index = json_crud_array.getNewNodeIndex();  
 *             
 *      #add a node to the array
 *          json_crud_array.addNode(index, newnode)
 *      
 *      #getting total no of element in the array
 *          json_crud_array.getTotalNode()
 *          
 *      #check a node exist or not
 *          json_crud_array.isNodeExist('mahedi')      
 *      
 *      #check a node exist or not
 *          json_crud_array.isNodeExist('ruzdi')
 *      
 *      #remove node by index
 *          json_crud_array.removeNodeByIndex(1)
 *          
 *      #remove node by key
 *          json_crud_array.removeNodeByKey('ruzdi')
 *          
 *      #search from the array
 *          json_crud_array.partialMatchByKey('ruz')
 *          
 *      #search from the array with limit
 *          json_crud_array.partialMatchByKey('ruz', 1)
 *          
 *          
 * 
 * @author Md. Ruzdi Islam  
 * ***************************************************************************
*/


var json_crud_array = {    
    
    
    /**
     * @function addNode 
     * @description this is for add a node
     * @param index : index of the new node like 0,1,2, etc .
     * @param newnode : new node object it is like {key:'ruzdi', ...... } 
     * @return true : if added  false : if failed
     */
    addNode: function(index, newnode){
        if( json_crud_array.isNodeExist(newnode.key) == false){
            myjson_array.array[index] = newnode;
            return true;
        }
        return false;

    },    
    
    /**
     * @function removeNodeByIndex 
     * @description this is for removing a node by array index
     * @param index : index of the new node like 0,1,2, etc .     
     * @return true : if removed  false : if failed
     */
    removeNodeByIndex: function(index){
        if(json_crud_array.getTotalNode() > 0){
            if(myjson_array.array.splice(index,1)){
                return true;
            }
        }
        return false;    
    },
    
    /**
     * @function removeNodeByKey 
     * @description this is for removing a node by array key
     * @param key : key of the each array
     * @return true : if removed  false : if failed
     */
    removeNodeByKey: function(key){
        var index = json_crud_array.isNodeExist(key);
        if(index != false){
            if(json_crud_array.removeNodeByIndex(index-1)){
                return true;
            }
        }
        return false;
    },
    
    /**
     * @function isNodeExist 
     * @description this is for chacking a node that it is exist or not
     * @param key : key of the each array
     * @return integer : node index if found else false
     */
    isNodeExist: function(key){
        var nodeIndex = false;
        for(var i=0; i<json_crud_array.getTotalNode(); i++){
            if(myjson_array.array[i].key.toString().toLowerCase() == key.toString().toLowerCase()){
                nodeIndex = i+1;
                break;
            }
        }
        return nodeIndex;

    },
    
    /**
     * @function getTotalNode 
     * @description this is for chacking total no of node/element in the array 
     * @return integer : total no of node
     */
    getTotalNode: function(){
        var index = false;
        if(myjson_array.array){
            index = myjson_array.array.length;
        }
        return index;
    },

    /**
     * @function getNewNodeIndex 
     * @description this is for getting the next empty index of the array
     * @return integer : new index of the array
     */
    getNewNodeIndex: function(){
        var newIndex = 0;
        var totalNode = json_crud_array.getTotalNode();
        if(totalNode != false){
            newIndex = totalNode;
        }
        return newIndex;
    },
    
    /**
     * @function partialMatchByKey 
     * @description this is for searching the array
     * @param index : string to match. this is the value of the key like 'ruzdi', 'mahedi'
     * @param limit : how many no of matching element you need 
     * @return json array :  if match found else  false :  if string is not match 
     */
    partialMatchByKey: function(match,limit){
        var isMatch = false;
        myjson_array.matches = [];        
        for(var i=0,j=0; i<json_crud_array.getTotalNode(); i++){                        
            if (myjson_array.array[i].key.toString().toLowerCase().indexOf(match.toString().toLowerCase()) >= 0){                
                myjson_array.matches[j] = myjson_array.array[i]; 
                j++;
                isMatch = true;                        
                
                if(limit && isMatch){
                    if(j>=limit){
                        return myjson_array.matches;
                    }
                }                
            }            
        }

        if(isMatch == true){
            return myjson_array.matches;
        }        
        return isMatch;

    }
    
};